var mqtt = require('mqtt')
var client = mqtt.connect('mqtt://127.0.0.1');

client.on('connect', function () {
    client.subscribe('IACMSB20173701040001', 'IACMPB20173701040001');
})
var hexStr = "685700b20100333730313034303030310000000017091208080801000033373031303430303031050500050f3139322e3034312e3033302e32343105303138383330303030303030303030303030303030303030303030c90d0a";
client.on('message', function (topic, message) {
    // message is Buffer
    var hexStr = message.toString();
    console.log(hexStr);
    getJsonData(hexStr);
    client.end()
})
var BCD = "170912080808";
var date = getDate(BCD);
console.log(date + "===============");
//将16进制的BCD转换为日期
function getDate(BCD) {
    var Y = "20" + BCD.substring(0, 2);
    var M = BCD.substring(2, 4);
    var D = BCD.substring(4, 6);
    var h = BCD.substring(6, 8);
    var m = BCD.substring(8, 10);
    var s = BCD.substring(10, 12);
    return Y + "-" + M + "-" + D + " " + h + ":" + m + ":" + s;
}

//获取16进制总的字符串
function getJsonData(hexStr) {
    var S = hexStr.substring(0, 2);
    var L = hexStr.substring(2, 6);
    var AFN = hexStr.substring(6, 8);
    var SN = hexStr.substring(8, 12);
    var ID = hexStr.substring(12, 32);
    var MSDN = hexStr.substring(32, 40);
    var TP = hexStr.substring(40, 52);
    var SN2 = hexStr.substring(52, 54);
    var DATA = hexStr.substring(54, (hexStr.length - 6));
    var CRC = hexStr.substring((hexStr.length - 6), (hexStr.length - 4));
    var E = "0d0a";
    var JsonData = {};
    JsonData.S = S;
    JsonData.L = L;
    JsonData.AFN = AFN;
    JsonData.SN = SN;
    JsonData.ID = ID;
    JsonData.MSDN = MSDN;
    JsonData.TP = TP;
    JsonData.SN2 = SN2;
    JsonData.DATA = DATA;
    JsonData.CRC = CRC;
    JsonData.E = E;
    if (AFN == "b2") {
        getB2Data(JsonData);
    } else if (AFN == "30") {
        getThirtyData(JsonData);
    } else if (AFN == "20") {
        getTwentyData(JsonData);
    }

}

//获取AFN=b2的data数据
function getB2Data(JsonData) {
    var data = JsonData.DATA

    var jsonB2Data = {};
    jsonB2Data.contro1 = data.substring(0, 2);//控制字节1
    jsonB2Data.contro2 = data.substring(2, 4);//控制字节2
    jsonB2Data.sb_id = data.substring(4, 24);//设备ID
    jsonB2Data.cj_jg = data.substring(24, 26);//采集间隔
    jsonB2Data.bc_jg = data.substring(26, 28);//保存间隔
    jsonB2Data.dsfs_jg = data.substring(28, 32);//定时发送间隔
    jsonB2Data.ip = data.substring(32, 76);//ip与端口号
    jsonB2Data.sfqcls_sj = data.substring(76, 78);//是否清楚历史数据
    jsonB2Data.sfhfchz = data.substring(78, 80);//是否恢复出厂值
    jsonB2Data.bl = data.substring(80, 120);
    console.log(JSON.stringify(jsonB2Data));
}

//获取AFN=30的Data数据
function getThirtyData(JsonData) {
    var data = JsonData.DATA;
    var dataLength = JsonData.SN2;
    dataLength = "03";
    dataLength = parseInt(dataLength, 16);
    var arrayThirtyData = [];
    for (var i = 0; i < dataLength; i++) {
        var JsonThirtyData = {};
        JsonThirtyData.sb_lx = data.substring((128 * i), (128 * i) + 2);//设备类型
        JsonThirtyData.dqsjbcts = data.substring((128 * i) + 2, (128 * i) + 10);//当前数据保存条数
        JsonThirtyData.T_sz = data.substring((128 * i) + 10, (128 * i) + 18);//温度
        JsonThirtyData.RH_sz = data.substring((128 * i) + 18, (128 * i) + 26);//湿度
        JsonThirtyData.BV = data.substring((128 * i) + 26, (128 * i) + 30);//电池电压
        JsonThirtyData.cj_jg = data.substring((128 * i) + 30, (128 * i) + 32);//当前采集间隔
        JsonThirtyData.channel1 = data.substring((128 * i) + 32, (128 * i) + 34);//通道1情况
        JsonThirtyData.l1_sz = data.substring((128 * i) + 34, (128 * i) + 42);//通道1电流值
        JsonThirtyData.channel2 = data.substring((128 * i) + 42, (128 * i) + 44);//通道2情况
        JsonThirtyData.l2_sz = data.substring((128 * i) + 44, (128 * i) + 52);//通道2电流值
        JsonThirtyData.channel3 = data.substring((128 * i) + 52, (128 * i) + 54);//通道3情况
        JsonThirtyData.l3_sz = data.substring((128 * i) + 54, (128 * i) + 62);//通道3电流值
        JsonThirtyData.channel4 = data.substring((128 * i) + 62, (128 * i) + 64);//通道4情况
        JsonThirtyData.l4_sz = data.substring((128 * i) + 64, (128 * i) + 72);//通道4电流值
        JsonThirtyData.so2_sz = data.substring((128 * i) + 72, (128 * i) + 80);//二氧化硫数据
        JsonThirtyData.cwm_1 = data.substring((128 * i) + 80, (128 * i) + 82);//错误1
        JsonThirtyData.cwm_1 = data.substring((128 * i) + 82, (128 * i) + 84);//错误2
        JsonThirtyData.xtsssj = data.substring((128 * i) + 84, (128 * i) + 96);//当前数据采集时间
        JsonThirtyData.bl = data.substring((128 * i) + 96, (128 * i) + 128);//保留16个字节
        arrayThirtyData.push(JsonThirtyData);
    }
    console.log(JSON.stringify(arrayThirtyData));
}

//获取AFN=20的
function getTwentyData(JsonData) {
    var data = JsonData.DATA;
    console.log(JsonData.SN2);
    var jsonTwentyData = {};
    jsonTwentyData.sb_lx = data.substring(0, 2);//设备类型
    jsonTwentyData.readflag = data.substring(2, 4);//是否读取
    jsonTwentyData.bv = data.substring(4, 8);//电池电压
    jsonTwentyData.cj_jg = data.substring(8, 10);//当前采集间隔
    jsonTwentyData.l1_sz = data.substring(10, 18);//数据1
    jsonTwentyData.t_sz = data.substring(18, 26);//温度
    jsonTwentyData.rh_sz = data.substring(26, 34);//湿度
    jsonTwentyData.cwm_1 = data.substring(34, 36);//错误1
    jsonTwentyData.cwm_2 = data.substring(36, 38);//错误2
    jsonTwentyData.lsbcshts = data.substring(38, 46);//历史保存条数
    jsonTwentyData.xtsssj = data.substring(46, 58);//系统实时时间
    jsonTwentyData.so2_sz = data.substring(58, 66);//二氧化硫数据
    jsonTwentyData.l2_sz = data.substring(66, 74);//电流2数据
    jsonTwentyData.l3_sz = data.substring(74, 82);//电流3数据
    jsonTwentyData.l4_sz = data.substring(82, 90);//电流4数据
    jsonTwentyData.fasong_ok = data.substring(90, 92);//发送是否完成

    console.log(JSON.stringify(jsonTwentyData));
}


/*
将16进制转换为浮点数
*/
function getFloat(data_hex) {
    var data_bit = parseInt(data_hex, 16).toString(2);//16进制转2进制

    var data_E = parseInt(data_bit.slice(0, 8), 2);  //slice表示数组的截取，并转化为十进制数

    var data_M = data_bit.slice(8, 64);

    var data_M_10 = 0.00;
    for (var i = 0; i < data_M.length; i++) {
        data_M_10 = data_M_10 + data_M[i] * Math.pow(2, (-1) * (i + 1));

    }
    console.log('十进制的尾数为：', data_M_10);

    var data_float = Math.pow(2, data_E - 127) * (1 + data_M_10);

    console.log(data_float);
}